(ns syncode.xchange.collector.rates
  (:use syncode.xchange.collector.configuration)
  (:use net.cgrand.enlive-html)
  (:import java.net.URL))
           
(defn rate-elem [rate-node n] ((first (select rate-node [[:td (nth-child n)]])) :content))

(defn rate-map [node positions] 
  { :currency (rate-elem node (positions :currency)),
    :denomination (rate-elem node (positions :denomination)),
    :buying (rate-elem node (positions :buying)),
    :selling (rate-elem node (positions :selling))})

(defn html-map [url] (-> url URL. html-resource))

(defn rates [exchanger] 
  (map 
    (fn [rate-node] (rate-map rate-node (positions exchanger)))
    (-> exchanger uris html-map ((rate-selectors exchanger)))))